import 'package:get/get.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/data/BannerEntity.dart';
import 'package:mtf/app/data/NewsEntity.dart';
import 'package:mtf/app/data/NoticeEntity.dart';
import 'package:mtf/app/data/VideoEntity.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/routes/app_pages.dart';
import 'package:mtf/app/utils/AppUtils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:url_launcher/url_launcher.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class newsController extends BaseController with StateMixin {
  var page =1;
  RefreshController refreshController =
      RefreshController(initialRefresh: false);

  RxList<ListBannerElement> bannerList = <ListBannerElement>[].obs;
  RxList<ListElement> noticeList = <ListElement>[].obs;
  RxList<ListVideoElement> videoList = <ListVideoElement>[].obs;
  RxList<ListNewsElement> newsList = <ListNewsElement>[].obs;
  var commentClassify="7";
  var videoClassify="8";

  void refreshData() async {
    reqData();
  }

  @override
  void onInit() {
    super.onInit();
    change(null, status: RxStatus.loading());
    reqData();
  }

  void loadData() {
    loadNewsList();
  }


  void reqData() async {
    page=1;
    var bannerResp = await HttpManager.reqBannerList();
    if(bannerResp.isSuccess()&&bannerResp.data!=null){
      bannerList.assignAll(bannerResp.data!.list);
      var comment=bannerResp.data?.indexTypeList?.firstWhere((element) => "day_comment"==element.typeEn);
      var video=bannerResp.data?.indexTypeList?.firstWhere((element) => "video_study"==element.typeEn);
      if(comment!=null){
        commentClassify=comment.typeId.toString();
      }
      if(video!=null){
        videoClassify=video.typeId.toString();
      }
    }
    refreshController.refreshCompleted();
    change(null, status: RxStatus.success());
    var noticeResp = await HttpManager.reqNoticeList(1);
    if(noticeResp.isSuccess()&&noticeResp.data!=null){
      noticeList.assignAll(noticeResp.data!.list);
    }
    change(null, status: RxStatus.success());
    var videoResp = await HttpManager.reqVideoListSize(1,2);
    if(videoResp.isSuccess()&&videoResp.data!=null){
      videoList.assignAll(videoResp.data!.list);
    }
    change(null, status: RxStatus.success());
    var newsResp=await HttpManager.reqNewsListSize(page);
    if(newsResp.isSuccess()&&newsResp.data!=null){
      newsList.assignAll(newsResp.data!.list);
    }
    change(null, status: RxStatus.success());
  }

  void loadNewsList() async{
    page+=1;
    var newsResp=await HttpManager.reqNewsList(page,"");
    if(newsResp.isSuccess()&&newsResp.data!=null){
      newsList.addAll(newsResp.data!.list);
      if(newsResp.data!.list.isEmpty){
        refreshController.loadNoData();
      }else{
        refreshController.loadComplete();
      }
    }else{
      refreshController.loadFailed();
    }

  }


  void clickNoticeList() {
    Get.toNamed(Routes.NOTICE_LIST);
  }

  void clickVideoDetail(ListVideoElement item){
    Get.toNamed(Routes.VIDEO_DETAIL,arguments: {
      "title": item.title,
      "time": AppUtils.dateToString(item.createdAt),
      "cover": item.imghead,
      "video":item.fileVideo,
      "content":item.content
    });
  }

  void clickRecordDetail(ListNewsElement item){
    Get.toNamed(Routes.EVALUATE_DETAIL,arguments: {
      "title": item.title,
      "time": AppUtils.dateToString(item.createdAt),
      "content": item.content,
      "cover": item.imgHead
    });
  }

  void clickBannerIntent(int index) async{
    var bean=bannerList[index];
    if(bean.article!=null&&bean.articleId!=null&&bean.articleId!>0){
      Get.toNamed(Routes.EVALUATE_DETAIL,arguments: {
      "title": bean.article?.title,
      "time": AppUtils.dateToString(bean.article?.createdAt),
      "content": bean.article?.content,
      "cover": bean.article?.imgHead
      });
    }else if(bean.href!=null&&bean.href!.isNotEmpty){
      await launch(bean.href??"");
    }
  }

  void clickVideoList() async{
    Get.toNamed(Routes.VIDEO_LIST,arguments: {"classifyId":videoClassify});
  }

  void clickCommentList() async{
    Get.toNamed(Routes.EVALUATE_RECORD,arguments: {"classifyId":commentClassify});
  }
}
